
class ColorMod:
    def __init__(self ,*val):
        if len(val) == 1:
            self.val = list(*val)
        else:
            self.val = [v for v in val]

        if type(self) is ColorMod:
            raise TypeError("不能使用ColorMod直接创建实例")

    def __str__(self):
        return f'{self.__class__.__name__}({self.val[0]},{self.val[1]},{self.val[2]})'


class RGB(ColorMod):

    def __init__(self, *val):
        super().__init__(*val)

    def HSV(self):
        r, g, b = self.val[0] / 255, self.val[1] / 255, self.val[2] / 255
        max_color = max(r, g, b)
        min_color = min(r, g, b)
        delta = max_color - min_color

        if delta == 0:
            h = 0
        elif max_color == r:
            h = (60 * ((g - b) / delta) + 360) % 360
        elif max_color == g:
            h = (60 * ((b - r) / delta) + 120) % 360
        else:
            h = (60 * ((r - g) / delta) + 240) % 360

        if max_color == 0:
            s = 0
        else:
            s = int(delta / max_color) * 100

        v = int(max_color) * 100
        return HSV(h, s, v)

    # 定义颜色深度为6位的RGB值
    def RGB_666(self):
        return (self.val[0] & 0x3F) << 2, (self.val[1] & 0x3F) << 2, (self.val[2] & 0x3F) << 2

    def RGB_565(self):
        return ((self.val[0] & 0x1F) << 3) | ((self.val[1] & 0x38) >> 3), ((self.val[1] & 0x07) << 5) | self.val[2] & 0x1F

    def RGB(self):
        return self.val


class HSV(ColorMod):
    def __init__(self, *val):
        super().__init__(*val)

    def HSV(self):
        return self.val

    def RGB_666(self):
        return RGB(self.RGB()).RGB_666()

    def RGB_565(self):
        return RGB(self.RGB()).RGB_565()

    def RGB(self):
        s = self.val[1] / 100
        v = self.val[2] / 100

        if s == 0:
            r = g = b = int(v * 255)
        else:
            h = self.val[0] / 60
            i = int(h)
            f = h - i
            p = int(v * (1 - s) * 255)
            q = int(v * (1 - s * f) * 255)
            t = int(v * (1 - s * (1 - f)) * 255)
            v = int(v * 255)

            if i == 0:
                r, g, b = v, t, p
            elif i == 1:
                r, g, b = q, v, p
            elif i == 2:
                r, g, b = p, v, t
            elif i == 3:
                r, g, b = p, q, v
            elif i == 4:
                r, g, b = t, p, v
            else:
                r, g, b = v, p, q

        return r, g, b


class RGB_666(ColorMod):
    def __init__(self, *val):
        super().__init__(*val)


class RGB_565(ColorMod):
    def __init__(self, *val):
        super().__init__(*val)


typical = {
    'red': RGB(255, 0, 0),
    'green': RGB(0, 255, 0),
    'blue': RGB(0, 0, 255),
    'white': RGB(255, 255, 255),
    'orange': RGB(255, 165, 0),  # 橙色
    'yellow': RGB(255, 255, 0),  # 黄色
    'cyan': RGB(0, 255, 255),  # 青色
    'purple': RGB(153, 61, 153),  # 紫色
    'pink': RGB(255, 192, 203),  # 粉色
    'brown': RGB(143, 72, 65),  # 棕色
    'black': RGB(0, 0, 0)
}


if __name__ == '__main__':
    red = HSV(300, 60, 60)
    print(red)
    print(red.RGB())
    print(RGB_666(red.RGB_666()))
